$(function() {  
    FastClick.attach(document.body);  
    typefaceLength();
});

/*
var currentPage = 1;
$(function(){
    $('.content').dropload({
        scrollArea : window, //滑动区域
        //上拉刷新
        domDown : {
            domClass   : 'dropload-down',
            domRefresh : '<div class="dropload-refresh">↑上拉加载更多...</div>',
            domLoad    : '<div class="dropload-load"><span class="loading"></span>加载中-请稍等...</div>',
            domNoData  : '<div class="dropload-noData">暂无数据-数据已加载完</div>'
        },
        loadDownFn : function(me){
            // 加载菜单的数据
            $.ajax({
                type: 'GET',
                url: rootPath+'/goods/queryBySortList',
                data : {"currPage":currentPage},
                dataType: 'json',
              	async : false,
                success: function(data,status){
                	//alert(JSON.stringify(data))
                	currentPage =data.currentPage;
                	currentPage++;
                 	var resultList = data.resultList;
                 	var beginIndex =data.beginIndex;
                 	var totalRecords = data.totalRecords; //总条数
                    var result = '';
                	if(resultList!=null && resultList.length>0){
                		var optionHtml = '';
                		$.each(resultList,function(i,goods){
							optionHtml+="<li class='fl'> "
									+" <a href='"+rootPath+"/goods/queryGoodsDetail?id="+goods.id+"'> "
									+" 		<img src='"+rootPath+"/image/pic5.png' alt='' /> "
									+" 		<p class='moreWord'>"+goods.name+"</p> "
									+" </a> "
									+" <p> "
									+" 		<a href='javascript:void(0);'>包邮</a> "
									+" 		<span><b>¥ "+goods.marketPrice+"</b></span> "
									+" </p> "
									+" <p > "
									+" 		<span>100%好评</span> "
									+" 		<span>月销"+goods.monthSaleMount+"笔</span> "
									+" </p> "
									+" </li>";
						});
                	}else{
                		//tab1LoadEnd = true;
                        // 锁定
                        me.lock();
                        // 无数据
                        me.noData();
                	}
					// 设置延迟时间加载，延迟1秒加载
					setTimeout(function(){
					    $("#searchGoodsListO").append(optionHtml);
					    // 每次数据加载完，必须重置
					    me.resetload();
					},3000);
                        
               	},
                error: function(xhr, type){
                    alert('Ajax error!');
                    // 即使加载出错，也得重置
                    me.resetload();
                }
            });
        }
    });
});*/
var index=0;
var a = 0;


var lis = document.querySelectorAll('.sear ul li');
var imgs = document.querySelectorAll('.sear ul li img');
var spans = document.querySelectorAll('.sear>ul>li span');
var span = document.getElementsByClassName('searWord')[0];
var value = '';
for (var i = 0; i< lis.length;i++) {
	(function(n){
		lis[n].onclick=function(){
			imgs[n].style.display = 'block';
			spans[n].style.color='rgb(202,14,84)'
			value = spans[n].innerHTML;
			span.innerHTML = value;
			$('.mengban').css({display:'none'});
			$('.sear ul').css({display:'none'});
			$('.searImg').attr('src',''+rootPath+'/image/down01.png')
			for (var j = 0; j<lis.length;j++) {
				if(j!=n){
					imgs[j].style.display = 'none';
					spans[j].style.color='rgb(51,51,51)'
				}
			}
		}
	})(i);
}


//综合排序部分
$('.sear span').click(function(){
	index++;
	if(index%2!=0){
		//$('body').css({overflow:'hidden'});
		//$('html').css({overflow:'hidden'});
	    $('.mengban').css({display:'block',position:'absolute',top:'39.5rem'});
	    $('.sear ul').css({display:'block'});
	    $('.searImg').attr('src',''+rootPath+'/image/up011.png');
	}else{
		$('body').css({overflow:'visible'});
		$('html').css({overflow:'visible'});
	    $('.mengban').css({display:'none'});
	    $('.sear ul').css({display:'none'});
	    $('.searImg').attr('src',''+rootPath+'/image/down01.png');
	}
	
});

//店铺
$('.sanWang>li').click(function(){
	//$('.searImg').attr('src',''+rootPath+'/image/down01.png')
	for(var i=0;i<$('.sanWang>li').length;i++){
		index = $('.sanWang>li').index(this)
	}
	$('.sanWang>li').css({color:'rgb(51,51,51)',border:'0'});
	$(this).css({color:'rgb(202,14,84)',borderBottom:'0.2rem solid rgb(202,14,84)'});
	if($('.sanWang>li>span').eq(index).text()=='价格'){
		var type = $('.sanWang').find('.storeCi').attr('data-id');
		a++;
		if(a%2!=0){
			bySort('4',type);
			$('.storeCi img').attr('src',''+rootPath+'/image/qh1.png');
		}else{
			bySort('5',type);
			$('.storeCi img').attr('src',''+rootPath+'/image/qh.png');
		}
	}else{
		
		$('.storeCi img').attr('src',''+rootPath+'/image/qh2.png');
	}
	
	if(index!=0){
		$('.searImg').attr('src',''+rootPath+'/image/down1.png');
		$('.sear ul').css({display:'none'});
		$('.mengban').css({display:'none'});
	}
});

/**当前页数***/
var currentPage = 2;

/***根据综合、销量、价格排序****/
function bySort(sort,type){
	var classId = $("#class_Id").val();
	var searchName = $("#searchName").val();
	$.ajax({
		url : rootPath+"/goods/queryBySortList",
		type: "POST",
		data : {"classId":classId,"sort":sort,"searchName":searchName},
		async: false,
		success: function(data,status){
			var resultMap = data.resultList;
			var currentPages = data.currentPage;
			var totalRecords = data.totalRecords;
			if(type=='1'){
				$("#searchGoodsListO").empty();
				var html = optionHtmlO(resultMap);
				html+="<li class='pageRefresh fl' onclick='clickRefresh"+'("1");'+"'>点击加载更多<img src='"+rootPath+"/image/huan.png' alt='刷新' /></li>"
				$("#searchGoodsListO").append(html);
				typefaceLength();
				/*currentPage++;
				onPage(currentPage);*/
			}else if(type=='2'){
				$("#searchGoodsListT").empty();
				var html = optionHtmlT(resultMap);
				html+="<li class='pageRefresh fl' onclick='clickRefresh"+'("2");'+"'>点击加载更多<img src='"+rootPath+"/image/huan.png' alt='刷新' /></li>"
				$("#searchGoodsListT").append(html);
				typefaceLength();
				/*currentPage++;
				onPage(currentPage);*/
			}
			currentPage=2;
		},
		error : function(xhr,status,e){
			alert("服务器出了问题，请检查！"+e);
		}
	});
}

/***商品拼装html*O*  图形展示**/
function optionHtmlO(resultMap){
	var optionHtml ="";
	$.each(resultMap,function(i,goods){
		var marketPrice = (Number(goods.marketPrice)).toFixed(2);
		var k = i+1;
		if(k%2!=0){
			optionHtml+="<li class='fl'> "
		}else{
			optionHtml+="<li class='fr'> "
		}
			optionHtml+=" <a href='"+rootPath+"/goods/queryGoodsDetail?id="+goods.id+"'> "
				var picture = goods.picture;
				if(picture!=null){
					optionHtml+=" <img src='"+baseFile+picture+"' alt=''/> "
				}else{
					optionHtml+=" <img src='"+rootPath+"/image/pic5.png' alt='' /> "
				}
				optionHtml+=" 		<p class='moreWord'>"+goods.name+"</p> "
				+" </a> "
				+" <p> <b>¥ "+marketPrice+"</b> </p>"
				+" <p > "
				+" 		<span>月销"+goods.monthSaleMount+"笔</span> "
				var proTitle = goods.proTitle;
				if(proTitle!=null && proTitle!="" ){
					 optionHtml+= " <a href='javascript:void(0);'>"+goods.proTitle+"</a> "
				}
				
				optionHtml+=" </p> "
				+" </li>";
	});
//	optionHtml+="<li class='pageRefresh' onclick='clickRefresh"+'("1");'+"'>点击加载更多<img src='"+rootPath+"/image/huan.png' alt='刷新' /></li>"
	return optionHtml;
}

/***商品拼装html*T*  列表展示**/
function optionHtmlT(resultMap){
	var optionHtml ="";
	$.each(resultMap,function(i,goods){
		var marketPrice = (Number(goods.marketPrice)).toFixed(2);
		var k = i+1;
		if(k%2!=0){
			optionHtml+="<li class='fl'> "
		}else{
			optionHtml+="<li class='fr'> "
		}
	//	optionHtml+="<li class='fr'> "
			optionHtml+=" <a href='"+rootPath+"/goods/queryGoodsDetail?id="+goods.id+"'> "
				var picture = goods.picture;
				if(picture!=null){
					optionHtml+=" <img src='"+baseFile+picture+"' alt='' class='evaluateGoodsI evaluateGoodsM'/> "
				}else{
					optionHtml+=" <img src='"+rootPath+"/image/pic184.png' alt='' class='evaluateGoodsI evaluateGoodsM'/> "
				}
				optionHtml+=" </a> "
				+" <div class='evaluateGoodsW'> "
				+"		<p class='moreWord'>"+goods.name+"</p> "
				var proTitle = goods.proTitle;
				if(proTitle!=null && proTitle!="" ){
					 optionHtml+= " <a href='javascript:void(0);'>"+goods.proTitle+"</a> "
				}
				optionHtml+="		<p class='prices'> "
				+"			<b>¥ "+marketPrice+"</b> "
				+"					<span>月销"+goods.monthSaleMount+"笔</span> "
	//			+"					<span>97%好评</span> "
				+"				</p> "
				+"			</div> "
				+" </li>";
	});
//	optionHtml+="<li class='pageRefresh' onclick='clickRefresh"+'("2");'+"'>点击加载更多<img src='"+rootPath+"/image/huan.png' alt='刷新' /></li>"
	return optionHtml;
}


function onPage(currentPage){
	$('.content').dropload({
	    scrollArea : window, //滑动区域
	    //上拉刷新
	    domDown : {
	        domClass   : 'dropload-down',
	        domRefresh : '<div class="dropload-refresh">↑上拉加载更多...</div>',
	        domLoad    : '<div class="dropload-load"><span class="loading"></span>加载中-请稍等...</div>',
	        domNoData  : '<div class="dropload-noData">暂无数据-数据已加载完</div>'
	    },
	    loadDownFn : function(me){
	        // 加载菜单的数据
	        $.ajax({
	            type: 'POST',
	            url: rootPath+'/goods/queryBySortList',
	            data : {"currPage":currentPage},
	            dataType: 'json',
	          	async : false,
	            success: function(data,status){
	            	//alert(JSON.stringify(data))
	            	currentPage =data.currentPage;
	            	currentPage++;
	             	var resultList = data.resultList;
	             	var beginIndex =data.beginIndex;
	             	var totalRecords = data.totalRecords; //总条数
	                var result = '';
	            	if(resultList!=null && resultList.length>0){
	            		var optionHtml = '';
	            		$.each(resultList,function(i,goods){
							optionHtml+="<li class='fr'> "
									+" <a href='"+rootPath+"/goods/queryGoodsDetail?id="+goods.id+"'> "
									+" 		<img src='"+rootPath+"/image/pic5.png' alt='' /> "
									+" 		<p class='moreWord'>"+goods.name+"</p> "
									+" </a> "
									+" <p> "
									+" 		<a href='javascript:void(0);'>"+goods.proTitle+"</a> "
									+" 		<span><b>¥ "+goods.marketPrice+"</b></span> "
									+" </p> "
									+" <p > "
									+" 		<span></span> "
									+" 		<span>月销"+goods.monthSaleMount+"笔</span> "
									+" </p> "
									+" </li>";
						});
	            	}else{
	            		//tab1LoadEnd = true;
	                    // 锁定
	                    me.lock();
	                    // 无数据
	                    me.noData();
	            	}
					// 设置延迟时间加载，延迟1秒加载
					setTimeout(function(){
					    $("#searchGoodsListO").append(optionHtml);
					    // 每次数据加载完，必须重置
					    me.resetload();
					},3000);
	                    
	           	},
	            error: function(xhr, type){
	                alert('Ajax error!');
	                // 即使加载出错，也得重置
	                me.resetload();
	            }
	        });
	    }
	});
}


/***商品列表*****/
function clickRefresh(refreshType){
	var classId = $("#class_Id").val();
	var searchName = $("#searchName").val();
	//console.log(refreshType);
    // 加载菜单的数据
    $.ajax({
        type: 'POST',
        url: rootPath+'/goods/queryBySortList',
        data : {"currPage":currentPage,"classId":classId,"searchName":searchName},
        dataType: 'json',
      	async : false,
        success: function(data,status){
        	//alert(JSON.stringify(data))
        	currentPage =data.currentPage;
        	currentPage++;
         	var resultList = data.resultList;
         	var beginIndex =data.beginIndex;
         	var totalRecords = data.totalRecords; //总条数
         	var optionHtml="";
        	if(resultList!=null && resultList.length>0){
        		if(refreshType == "1"){
        			optionHtml = optionHtmlO(resultList)
            	}else if(refreshType == "2"){
            		optionHtml = optionHtmlT(resultList);
        		}
        	}else{
        		$(".pageRefresh").text("数据加载完毕！！！")
        		$(".pageRefresh").removeAttr("onclick");  //移除事件
        	}	
        	$(".pageRefresh").before(optionHtml);
        	typefaceLength();
			// 设置延迟时间加载，延迟1秒加载
			/*setTimeout(function(){
			    $("#searchGoodsListO").append(optionHtml);
			    // 每次数据加载完，必须重置
			    me.resetload();
			},3000);*/
       	},
        error: function(xhr, type){
            alert('Ajax error!');
        }
    });
	
}



function typefaceLength(){
	var str = $('.moreWord').text().substr(0,13)+'...';
	$('.moreWord').text(str);
}